/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business;

import Model.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
//import java.sql.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author Nguyen Hung
 */
public class Order {

    private ArrayList<LineItem> lineItems = new ArrayList<LineItem>();
    private Date date;
    private Customer customer;
    private String note;
    private String idOrder;
    private String paytype;
    private String status;

    

    public String getIdOrder() {
        return idOrder;
    }

    public void setIdOrder(String idOrder) {
        this.idOrder = idOrder;
    }

    public Customer getCustomer() {
        return customer;
    }

    public Date getDate() {
        return date;
    }

    public ArrayList<LineItem> getLineItems() {
        return lineItems;
    }

    public Order(Date date, Customer customer, String note, String paytype, String status) {
        this.date = date;
        this.customer = customer;
        this.note = note;
        this.idOrder = customer.getCustomerID() + "" + date.toString();;
        this.paytype = paytype;
        this.status = status;
    }

    public void addAll(ArrayList<LineItem> item) {
        this.lineItems.addAll(item);
    }

    public String getNote() {
        return note;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setLineItems(ArrayList<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Order(Customer customer, String note, Date date) {
//        GregorianCalendar calendar = new GregorianCalendar();
        this.date = date;
        this.customer = customer;
        this.note = note;
        idOrder = customer.getCustomerID() + "" + date.toString();
    }

    public String getPaytype() {
        return paytype;
    }

    public String getStatus() {
        return status;
    }

      public Order(Customer customer, Date date) {
//        GregorianCalendar calendar = new GregorianCalendar();
        this.date = date;
        this.customer = customer;       
        idOrder = customer.getCustomerID() + "" + date.toString();
    }

    public Order() {
    }

    public boolean save() {
        boolean result = false;
        try {

            Connection connection = DAO.getConnection();
            String sql = "insert into shoppingonline.Order values(?, ?, ?, ?, ?, ?, )";
            PreparedStatement ps = connection.prepareStatement(sql);

            ps.setString(1, idOrder);
            ps.setDate(2, new java.sql.Date(date.getTime()));
            ps.setInt(3, customer.getCustomerID());
            ps.setString(4, note);
            ps.setString(5, status);
            ps.setString(6, paytype);
            ps.execute();
            for (LineItem item : lineItems) {
                item.save(idOrder);
            }


        } catch (SQLException ex) {
            result = false;
            ex.printStackTrace();
        }
        return result;
    }


    public double getTotal() {
        double result = 0.0;
        for (LineItem item : lineItems) {
            result += item.getTotal();
        }
        return result;
    }
    public static void main(String[] arg ){
//        Product product = new Product();
//        product.setProductCode("5");
//        LineItem item = new LineItem(product, 1);
//        Customer customer = new Customer("Hung", "Manchester", "+82325888", "haha", "1881155555");
//        Order order = new Order(customer, "", new Date(2011,5,6));
//        ArrayList<LineItem> items = new ArrayList<LineItem>();
//        items.add(item);
//        order.setLineItems(items);
//        customer.save();
//        order.save();

        GregorianCalendar calendar = new GregorianCalendar();
        Date date = new Date(calendar.get(GregorianCalendar.YEAR), calendar.get(GregorianCalendar.MONTH), calendar.get(GregorianCalendar.DATE));
        System.out.println(date.getYear());


    }
}
